import { forEachValue } from "../utils"

export default class Module {
  constructor(rootModule) {
    this._raw = rootModule
    this._children = {}
    this.state = rootModule.state
  }
  // 是否写了 namespaced
  get namespaced() {
    return !!this._raw.namespaced
  }

  getChild(key) {
    return this._children[key]
  }

  addChild(key, module) {
    this._children[key] = module
  }

  forEachMutation(callback) {
    this._raw.mutations && forEachValue(this._raw.mutations, callback)
  }

  forEachAction(callback) {
    this._raw.actions && forEachValue(this._raw.actions, callback)
  }

  forEachGetters(callback) {
    this._raw.getters && forEachValue(this._raw.getters, callback)
  }

  forEachChildren(callback) {
    if (this._children) {
      forEachValue(this._children, callback)
    }
  }
}